import { getPlantsApi, getYearsApi, getWorkShopsApi } from "@/api/plant/index";
import { getWeeksInYear } from "@/utils/dateUtils";
import request from "@/utils/request";

// 获工厂列表
export function getPlantList() {
  return new Promise(async (resolve, reject) => {
    const { data } = await getPlantsApi();
    resolve(data);
  });
}

// 获年份列表
export function getYearList() {
  return new Promise(async (resolve, reject) => {
    const { data } = await getYearsApi();
    resolve(data);
  });
}

// 获工作中心列表
export function getWorkShopList(id: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await getWorkShopsApi(id);
    resolve(data);
  });
}

// 获取其他工作中心
export function getOtherWorkShopList(id: string, type: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mes/mesworkshop/getOtherList",
      method: "post",
      data: {
        plantId: id,
        groupType: type,
      },
    });
    resolve(data);
  });
}

// 获取首工序工作中心
export function getFirstWorkShopList(id: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mes/mesworkshop/getFirstList",
      method: "post",
      data: {
        plantId: id,
      },
    });
    resolve(data);
  });
}

// 获取工艺路线分组
export function getMesRouteGroupList() {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mes/mesroutinggroup/getAvailableList",
      method: "post",
      data: {},
    });
    resolve(data);
  });
}

// 获取委外供应商列表
export function getSupplierList(plantId?: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: `/mdm/mdmpartner/getList?plantId=${plantId}`,
      method: "get",
      data: {},
    });
    resolve(data);
  });
}

// 获取承运商列表
export function getGeneralCarrierList() {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmgeneralcarrier/getGeneralCarrierList",
      method: "post",
      data: {
        enabled: 1,
      },
    });
    resolve(data);
  });
}

// 获取承运商车辆列表
export function getVehicleList(generalCarrierId: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmgeneralcarrier/getVehicleList",
      method: "post",
      data: {
        generalCarrierId: generalCarrierId,
        enabled: 1,
      },
    });
    resolve(data);
  });
}

// 获取承运商司机列表
export function getDriverList(generalCarrierId: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmgeneralcarrier/getDriverList",
      method: "post",
      data: {
        generalCarrierId: generalCarrierId,
        enabled: 1,
      },
    });
    resolve(data);
  });
}

// 获取班组编号列表
export function getCrewList(id: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mes/mescrew/getCrew",
      method: "post",
      data: {
        plantId: id,
        enabled: 1,
      },
    });
    resolve(data);
  });
}

// 获取物料下拉列表(工艺路线)
export function getMaterialList(plantId: string, workCenterId: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mes/mesroutingallocation/getList",
      method: "post",
      data: {
        workCenterId,
        plantId,
      },
    });
    resolve(data);
  });
}

// 获取物料下拉列表(主数据)
export function getMaterials(plantId: string, workCenterId: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmmaterial/getList",
      method: "post",
      data: {
        workCenterId,
        plantId,
      },
    });
    resolve(data);
  });
}

// 获取我的物料下拉列表(主数据)
export function getMyMaterials() {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmmaterial/getListByUser",
      method: "get",
    });
    resolve(data);
  });
}

// 获取工作中心详情、最大负荷、日历类型
export function getWorkCenterDetail(id: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mes/mesworkshop/get/" + id,
      method: "get",
      data: {},
    });
    resolve(data);
  });
}

// 获取班产量
export function getCurrentYield(
  routingAllocationId: string,
  workCenterId: string
) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mes/mesworkorder/getRoutingProcessYield",
      method: "post",
      data: {
        routingAllocationId,
        workCenterId,
      },
    });

    resolve(data);
    // resolve(3000);
  });
}

// 获取休息日
export function getFreeDetails(id: string, year: string | number) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmworkcalendar/getWithDetails",
      method: "post",
      data: {
        plantId: id,
        workCalendarYear: year,
      },
    });
    resolve(data);
  });
}

// 获取周列表
export function getMonthList(year: number) {
  const weekCount = getWeeksInYear(year);
  const weekList = [];
  for (let i = 1; i <= weekCount; i++) {
    weekList.push({
      value: i,
      label: `第${i}周`,
    });
  }
  return weekList;
}

// 获仓库列表 --- 当前登陆人 详情 公司id
export function getWarehouseList(id?: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/wms/warehouse/getList",
      method: "get",
      params: {
        organizationId: id,
      },
    });
    resolve(data);
  });
}

// 获存储类型列表
export function getStorageTypeList(warehouseId: string) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/wms/storagetype/getList",
      method: "get",
      params: { warehouseId: warehouseId },
    });
    resolve(data);
  });
}

// 获取班制班次
export function getDateshiftCount(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mes/messhiftschedule/getDateshiftCount",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

/** 打开标准作业常用api */

// 获取作业类型列表
export function getStandardTypesApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/eam/eamsoptype/getlist",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取作业设备列表
export function getStandardDevicesApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/eam/eamAssetType/getAvailableTree",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

export function getAvailableListApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/eam/eamAssetType/getAvailableList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取制造商列表
export function getMakerListApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/eam/eammaker/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取品牌型号列表
export function getBrandModelListApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/eam/eamAssetmodel/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取自定义字段列表
export function getCustemParamsApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/eam/eamcustom/getConfiguration",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取金泰班组下拉列表
export function getCrewListApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/eam/eamTeam/getAvailableList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取金泰班组人员下拉列表
export function getCrewMemberListApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/eam/eamTeam/getDetails",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取故障模式列表
export function getFaultModeListApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: `/eam/eamfaultmode/getAvailableList`,
      method: "post",
      data: params || {},
    });
    resolve(data);
  });
}
// 获取选择工序列表
export function getMdmprocessListApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmprocess/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取产品列表
export function getMdmproductinfosApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmproductinfo/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取项目列表
export function getMdmprojectinfosApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmprojectinfo/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 获取检具列表
export function getMdminspectiontoolApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdminspectiontool/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 通过工序查询设备
export function getMdmProcessEquipApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmprocessequip/getByProcess",
      method: "get",
      params: params,
    });
    resolve(data);
  });
}

export function getMdmProcessEquipApi1(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmprocessequip/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}

// 通过项目查询工序
export function getMdmProcessProjectApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdmprocess/getByProject",
      method: "get",
      params: params,
    });
    resolve(data);
  });
}

// 特性值获取
export function getSpecificListApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/dmpDosPcm/pcmmeasurefeatures/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}
export function getMdmProcessEquipApi3(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/dmpDosPcm/equipmentInfo/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}
// 获取检具分组列表
export function getMdminspectionGroupApi(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mdm/mdminspectiontoolgroup/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}
// 获取产品列表
export function getMdmproductinfosApi1(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/dmpDosPcm/psmequipmentproduct/getList",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}
// 特性值获取1
export function getSpecificListApi1(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/dmpDosPcm/pcmmeasurefeaturesgroup/getNames",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}
// 获取产品列表
export function getMdmproductinfosApi2(params: any) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/dmpDosPcm/psmparammonitorrule/exist/product/options",
      method: "post",
      data: params,
    });
    resolve(data);
  });
}
